//
//  MyFavListViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/6/2.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "MyFavListViewController.h"
#import "FavListCell.h"
#import "FavViewModel.h"
#import "FinderDetailViewController.h"
@interface MyFavListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
}
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property(nonatomic,strong)FavViewModel *favViewModel;
@property(nonatomic,strong)NSMutableArray *favList;
@property(nonatomic,assign)BOOL isRequest;
@end

@implementation MyFavListViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"我的收藏";
    [self configTableView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    if (!self.isRequest) {
        [self.favViewModel fetchFavListWithCmp:^(NSMutableArray * _Nonnull favList) {
            [self.favList addObjectsFromArray:favList];
            [self.mainTableView reloadData];
        }];
        self.isRequest = YES;
    }
}
-(void)configTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [UIView new];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"FavListCell" bundle:nil] forCellReuseIdentifier:@"FavListCell"];
    self.mainTableView.showsVerticalScrollIndicator = NO;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainTableView.backgroundColor = [UIColor clearColor];
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.favList.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FavListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FavListCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    if (indexPath.row==0) {
        [cell.bgView setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
    }else if (indexPath.row<2-1){
        [cell.bgView setImage:[[UIImage imageNamed:@"middleBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"middleBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"middleBg"].size.height/2]];
    }else{
        [cell.bgView setImage:[[UIImage imageNamed:@"bottomBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"bottomBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"bottomBg"].size.height/2]];
    }
    [cell setCellValue:[self.favList objectAtIndex:indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FinderDetailViewController *detail = [[FinderDetailViewController alloc]init];
    FinderDetailModel *model = [self.favList objectAtIndex:indexPath.row];
    detail.finderId = [NSString stringWithFormat:@"%.0f",model.internalBaseClassIdentifier];
    [self.rt_navigationController pushViewController:detail animated:YES complete:nil];
}
#pragma mark - instance
-(FavViewModel*)favViewModel{
    if (!_favViewModel) {
        _favViewModel = [[FavViewModel alloc]init];
    }
    return _favViewModel;
}
-(NSMutableArray*)favList{
    if (!_favList) {
        _favList = [[NSMutableArray alloc]init];
    }
    return _favList;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
